<?php

namespace app\admin\controller\user;

use app\common\controller\Backend;
use think\Db;
/**
 * 会员管理
 *
 * @icon fa fa-user
 */
class User extends Backend
{

    protected $relationSearch = true;


    /**
     * @var \app\admin\model\User
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('User');
    }

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->with('group')
                ->where($where)
                ->order($sort, $order)
                ->count();
            $list = $this->model
                ->with('group')
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            foreach ($list as $k => $v) {
                $v->hidden(['password', 'salt']);
            }
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 编辑
     */
    public function edit($ids = NULL)
    {
        $row = $this->model->get($ids);
        if (!$row)
            $this->error(__('No Results were found'));
        $this->view->assign('groupList', build_select('row[groupid]', \app\admin\model\UserGroup::column('id,name'), $row['groupid'], ['class' => 'form-control selectpicker']));
        return parent::edit($ids);
    }
    public function commission(){
        $id=$this->request->param('ids');
        $data=array();

        $fid=db::name('user')->where('id='.$id)->value('fatherid');
        $where=array();
        $where['fatherid']=$id;
        $yj=db::name('yonjin_jl')->where($where)->sum('yonjin');//echo $yj;exit;
        $where['createtime']=array(array('gt',strtotime(date('Ymd'))),array('lt',strtotime(date('Ymd'))+86400));//strtotime(date('Ymd'));
        $todayyj=db::name('yonjin_jl')->where($where)->sum('yonjin');
        $where['createtime']=array(array('gt',strtotime(date('Ymd'))-86400),array('lt',strtotime(date('Ymd'))));//strtotime(date('Ymd'))-86400;
        $yesyj=db::name('yonjin_jl')->where($where)->sum('yonjin');


        $yjlist=db::name('yonjin_jl')->where('fatherid='.$id)->field('uid,yonjin,createtime')->select();
		foreach ($yjlist as $keyss => $valuess) {
        $yjlist[$keyss]['yonjin']=$valuess['yonjin']/100;
      }
        $data['yjlist']=$yjlist;
        $data['yj']=$yj/100;
        $data['todayyj']=$todayyj/100;
        $data['yesyj']=$yesyj/100;
        $data['id']=$id;
        $data['fid']=$fid;

        $this->assign($data);
        return $this->view->fetch();
    }

    public function xiaji(){
        $id=$this->request->param('ids');
        $data=array();

        $fid=db::name('user')->where('id='.$id)->value('fatherid');
        $where=array();
        $where['fatherid']=$id;
        $yj=db::name('yonjin_jl')->where($where)->sum('yonjin');//echo $yj;exit;
        $where['createtime']=array(array('gt',strtotime(date('Ymd'))),array('lt',strtotime(date('Ymd'))+86400));//strtotime(date('Ymd'));
        $todayyj=db::name('yonjin_jl')->where($where)->sum('yonjin');
        $where['createtime']=array(array('gt',strtotime(date('Ymd'))-86400),array('lt',strtotime(date('Ymd'))));//strtotime(date('Ymd'))-86400;
        $yesyj=db::name('yonjin_jl')->where($where)->sum('yonjin');

        $yjlist=array();
        $yjlist=$this->mydaili($id);
        //print_r($yjlist['yjlist']);exit;
       
        $data['yjlist']=$yjlist['yjlist'];
        $data['yj']=$yj/100;
        $data['todayyj']=$todayyj/100;
        $data['yesyj']=$yesyj/100;
        $data['id']=$id;
        $data['fid']=$fid;

        $this->assign($data);
        return $this->view->fetch();
    }

    public function mydaili($uid=0){
        $yjlist=array();

       

        
        if($uid>0){
        //$uid=$uid>0?$uid:$this->_userid;
			$listones['fatherid']=$uid;
                $listone=db::name('user')->where($listones)->field('id,nickname,createtime')->select();
                foreach ($listone as $keyone => $valueone) {
                    $listone[$keyone]['dengji']=1;
                }
                $yjlist=$listone;
          
            $xtime=time()-600;

            $fmap['fatherid']=$uid;

        //  $fmap['usertype']=2;

            $father=Db::name('user')->field('id')->where($fmap)->select();

            $data['onefather']=count($father);

            $idx='';

            foreach($father as $key=>$vo){

             $idx.=$key==0?$vo['id']:','.$vo['id'];

            }

            $data['onefatherids']=$idx;
            

            $gfather=array();

            if($idx){
                $listtwos['fatherid']=array('in',$idx);
            $listtwo=db::name('user')->where($listtwos)->field('id,nickname,createtime')->select();
            foreach ($listtwo as $keytwo => $valuetwo) {
                $listtwo[$keytwo]['dengji']=2;
            }
            $yjlist=array_merge($yjlist,$listtwo);

                $gmap['fatherid']=array('in',$idx);
                $gfather=Db::name('user')->field('id')->where($gmap)->select();

            }
            $data['twofather']=count($gfather);

            $idx='';

            foreach($gfather as $key=>$vo){

             $idx.=$key==0?$vo['id']:','.$vo['id'];

            }
            $data['twofatherids']=$idx;
            


            $xfather=array();

            if($idx){

           $listthrs['fatherid']=array('in',$idx);
            $listthr=db::name('user')->where($listthrs)->field('id,nickname,createtime')->select();
            foreach ($listthr as $keythr => $valuethr) {
                $listthr[$keythr]['dengji']=3;
            }
            $yjlist=array_merge($yjlist,$listthr);

                $gmap['fatherid']=array('in',$idx);
                
                $xfather=Db::name('user')->field('id')->where($gmap)->select();

            }

            $data['thrfather']=count($xfather);

            $idx='';

            foreach($xfather as $key=>$vo){

             $idx.=$key==0?$vo['id']:','.$vo['id'];

            }
            $data['thrfatherids']=$idx;
          

            $xxfather=array();

            if($idx){

            $listfors['fatherid']=array('in',$idx);
            $listfor=db::name('user')->where($listfors)->field('id,nickname,createtime')->select();
            foreach ($listfor as $keyfor => $valuefor) {
                $listfor[$keyfor]['dengji']=4;
            }
            $yjlist=array_merge($yjlist,$listfor);


                $gmap['fatherid']=array('in',$idx);
                
                $xxfather=Db::name('user')->field('id')->where($gmap)->select();

            }

            $data['forfather']=count($xxfather);



            $idx='';

            foreach($xxfather as $key=>$vo){

             $idx.=$key==0?$vo['id']:','.$vo['id'];

            }
            $data['forfatherids']=$idx;
            

            $xxxfather=array();

            if($idx){
           $listfivs['fatherid']=array('in',$idx);
            $listfiv=db::name('user')->where($listfivs)->field('id,nickname,createtime')->select();
            foreach ($listfiv as $keyfiv => $valuefiv) {
                $listfiv[$keyfiv]['dengji']=5;
            }
            $yjlist=array_merge($yjlist,$listfiv);


                $gmap['fatherid']=array('in',$idx);
                
                $xxxfather=Db::name('user')->field('id')->where($gmap)->select();

            }

            $data['fivfather']=count($xxxfather);



            $idx='';

            foreach($xxxfather as $key=>$vo){

             $idx.=$key==0?$vo['id']:','.$vo['id'];

            }
            $data['fivfatherids']=$idx;
            

            $xxxxfather=array();

            if($idx){

            /*$listsixs['fatherid']=array('in',$idx);
            $listsix=db::name('user')->where($listsixs)->field('id,nickname,createtime')->select();
            foreach ($listsix as $keysix => $valuesix) {
                $listsix[$keysix]['dengji']=6;
            }
            $yjlist=array_merge($yjlist,$listsix);*/


                $gmap['fatherid']=array('in',$idx);
                
                $xxxxfather=Db::name('user')->field('id')->where($gmap)->select();

            }

            $data['sixfather']=count($xxxxfather);



             $idx='';

            foreach($xxxxfather as $key=>$vo){

             $idx.=$key==0?$vo['id']:','.$vo['id'];

            }
            $data['sixfatherids']=$idx;
            

            $xxxxxfather=array();

            if($idx){

               /* $listsevs['fatherid']=array('in',$idx);
            $listsev=db::name('user')->where($listsevs)->field('id,nickname,createtime')->select();
            foreach ($listsev as $keysev => $valuesev){
                $listsev[$keysev]['dengji']=7;
            }
            $yjlist=array_merge($yjlist,$listsev);*/



                $gmap['fatherid']=array('in',$idx);
                
                $xxxxxfather=Db::name('user')->field('id')->where($gmap)->select();

            }

            $data['sevfather']=count($xxxxxfather);



            $idx='';

            foreach($xxxxxfather as $key=>$vo){

             $idx.=$key==0?$vo['id']:','.$vo['id'];

            }
            $data['sevfatherids']=$idx;
            

            $eigfather=array();

            if($idx){

             /* $listeigs['fatherid']=array('in',$idx);
            $listeig=db::name('user')->where($listeigs)->field('id,nickname,createtime')->select();
            foreach ($listeig as $keyeig => $valueeig){
                $listeig[$keyeig]['dengji']=8;
            }
            $yjlist=array_merge($yjlist,$listeig);*/


                $gmap['fatherid']=array('in',$idx);
                
                $eigfather=Db::name('user')->field('id')->where($gmap)->select();

            }

            $data['eigfather']=count($eigfather);

            $idx='';

            foreach($eigfather as $key=>$vo){

             $idx.=$key==0?$vo['id']:','.$vo['id'];

            }
            $data['eigfatherids']=$idx;
            


            $nigfather=array();

            if($idx){

                /*$listeigs['fatherid']=array('in',$idx);
            $listeig=db::name('user')->where($listeigs)->field('id,nickname,createtime')->select();
            foreach ($listeig as $keyeig => $valueeig){
                $listeig[$keyeig]['dengji']=8;
            }
            $yjlist=array_merge($yjlist,$listeig);*/

            
                $gmap['fatherid']=array('in',$idx);
                
                $nigfather=Db::name('user')->field('id')->where($gmap)->select();

            }

            $data['nigfather']=count($nigfather);

            $idx='';

            foreach($nigfather as $key=>$vo){

             $idx.=$key==0?$vo['id']:','.$vo['id'];

            }
            $data['nigfatherids']=$idx;
            $tenfather=array();

            if($idx){
                $gmap['fatherid']=array('in',$idx);
                
                $tenfather=Db::name('user')->field('id')->where($gmap)->select();

            }

            $data['tenfather']=count($tenfather);

            $idx='';

            foreach($tenfather as $key=>$vo){

             $idx.=$key==0?$vo['id']:','.$vo['id'];

            }
            $data['tenfatherids']=$idx;
            $elefather=array();

            if($idx){
                $gmap['fatherid']=array('in',$idx);
                
                $elefather=Db::name('user')->field('id')->where($gmap)->select();

            }

            $data['elefather']=count($elefather);

            $idx='';

            foreach($elefather as $key=>$vo){

             $idx.=$key==0?$vo['id']:','.$vo['id'];

            }
            $data['elefatherids']=$idx;
            $twefather=array();

            if($idx){
                $gmap['fatherid']=array('in',$idx);
                
                $twefather=Db::name('user')->field('id')->where($gmap)->select();

            }

            $data['twefather']=count($twefather);

            $idx='';

            foreach($twefather as $key=>$vo){

             $idx.=$key==0?$vo['id']:','.$vo['id'];

            }
            $data['twefatherids']=$idx;
            $thifather=array();

            if($idx){
                $gmap['fatherid']=array('in',$idx);
                
                $thifather=Db::name('user')->field('id')->where($gmap)->select();

            }

            $data['thifather']=count($thifather);


            $idx='';

            foreach($thifather as $key=>$vo){

             $idx.=$key==0?$vo['id']:','.$vo['id'];

            }
            $data['thifatherids']=$idx;
            $foufather=array();

            if($idx){
                $gmap['fatherid']=array('in',$idx);
                
                $foufather=Db::name('user')->field('id')->where($gmap)->select();

            }

            $data['foufather']=count($foufather);

            $idx='';

            foreach($foufather as $key=>$vo){

             $idx.=$key==0?$vo['id']:','.$vo['id'];

            }
            $data['foufatherids']=$idx;
            $fiffather=array();

            if($idx){
                $gmap['fatherid']=array('in',$idx);
                
                $fiffather=Db::name('user')->field('id')->where($gmap)->select();

            }
            $data['fiffather']=count($fiffather);
        }
        $data['yjlist']=$yjlist;

        return $data;

    }

}
